<?php
/* @var $this ProjectController */
/* @var $model Project */

$this->breadcrumbs=array(
	'Projects'=>array('index'),
	$model->id,
);

$this->menu=array(
    array('label'=>'Add Item to Project', 'url'=>array('projectItem/create', 'id'=>$model->id )),
    array('label'=>'List Project', 'url'=>array('index')),
	array('label'=>'Create Project', 'url'=>array('create')),
	array('label'=>'Update Project', 'url'=>array('update', 'id'=>$model->id)),
	array('label'=>'Delete Project', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
	array('label'=>'Manage Project', 'url'=>array('admin')),
);
?>

<?php 

if(isset($_GET['projectItemid'])){
	$projectitemid = $_GET['projectItemid']; 
		
     $sql = "Select * from project_item where id = '$projectitemid'";
     $dataReader =  Yii::app()->db->createCommand($sql)->query();
     while(($row = $dataReader->read())!==false) {$pull = $row;}
	 $item_id=$pull['item_id'];
	 
	 
	 $sql2 = "Select unit_price from item where id = '$item_id'";
     $dataReader =  Yii::app()->db->createCommand($sql2)->query();
	 while(($row = $dataReader->read())!==false) {$pull2 = $row;}
	  
	 
	$request_amount = ($pull['qty_request'] * $pull2['unit_price']);	
	$used_amount = ($pull['qty_used'] * $pull2['unit_price']);
	$savings =  	($request_amount - $used_amount );
		
	
$sql1 = "update project_item set qty_request_amount='$request_amount' where id='$projectitemid'";
$dataReader =  Yii::app()->db->createCommand($sql1)->query();
		
$sql3 = "update project_item set qty_used_amount='$used_amount' where id='$projectitemid'";
$dataReader =  Yii::app()->db->createCommand($sql3)->query();
		
$sql4 = "update project_item set total_savings='$savings' where id='$projectitemid'";
$dataReader =  Yii::app()->db->createCommand($sql4)->query();

 $this->redirect(array('/project/view','id'=> $model->id));

}else{
echo '';
}

?>

<h1>Project:  <?php echo $model->project_name; ?></h1>

<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
		array('label' => 'Lead Man', 'value' => $model->point_person),
		'start_date',
		'end_date',
		array('label' => 'Total Project Cost', 'value' => 'Php ' .$model->total_cost),
		array('label' => 'Total Expenses', 'value' => 'Php ' .$model->total_expenses),
		array('label'=>'User', 'value'=>$model->user->fname .' '. $model->user->lname),
		
		
	),
)); 
?>

<br>
<?php 
$config = array();
$dataProvider = new CArrayDataProvider($rawData=$model->projectItems, $config); ?>
<br>
<h1>Items</h1>
<?php
$this->widget('zii.widgets.grid.CGridView', array('dataProvider'=>$dataProvider, 'columns'=>array(
		array('name'=>'Item', 'header'=> 'Items', 'value'=>'$data->item->item_desc'),
		array('name' => 'Qty Request', 'value' => '$data->qty_request' .' ." " . $data->item->unit '),
		array('name' => 'Total Request Amount', 'value' => ' "Php" ." " .$data->qty_request_amount ' ),
		array('name' => 'Qty Used', 'value' => '$data->qty_used' .' ." " .$data->item->unit ' ),
		array('name' => 'Total Used Amount', 'value' => ' "Php" ." " .$data->qty_used_amount '),
		array('name' => 'Total Savings', 'value' => ' "Php" ." " .$data->total_savings '),
		array(
            'class'=>'CButtonColumn'
            , 'viewButtonUrl'=>'Yii::app()->createUrl("/ProjectItem/view", array("id"=>$data["id"]))'
            , 'updateButtonUrl'=>'Yii::app()->createUrl("/ProjectItem/update", array("id"=>$data["id"]))'
            , 'deleteButtonUrl'=>'Yii::app()->createUrl("/ProjectItem/delete", array("id"=>$data["id"]))'
            )
			
    )
));


$total = Yii::app()->db->createCommand("SELECT SUM(`qty_request_amount`) AS `qty_request_amount` , SUM(`qty_used_amount`) AS `qty_used_amount`, SUM(`total_savings`) AS `total_savings` FROM `project_item` where `project_id` = '$model->id'")->queryAll();




if(isset($total[0]['qty_request_amount'])){
echo '<div id="yw3" class="grid-view">';
 echo '<table class="items">';
      echo '<th >Total Amount Requested</th><td class ="odd">'.' Php ' .($total[0]['qty_request_amount']).'</td></tr>'; 
	  echo '<th>Total Amount Used</th><td class ="odd">'.' Php ' .($total[0]['qty_used_amount']).'</td></tr>'; 
	  echo '<th>Total Project Savings</th><td class ="odd">'.' Php ' .($total[0]['total_savings']).'</td></tr>';	 echo '</table></div>';
		}else{
    	echo '';}
		
	?>


<br>
<br>
<?php 
$config = array();
$dataProvider = new CArrayDataProvider($rawData=$model->pullOuts, $config); ?>
<h1>Inventory Pull Out</h1>
<?php
$this->widget('zii.widgets.grid.CGridView', array('dataProvider'=>$dataProvider, 'columns'=>array(
		array('name'=>'Item', 'header'=> 'Items', 'value'=>'$data->inventory->item->item_desc'),
		array('name' => 'Qty Collected', 'value' => '$data->qty' .' ." " . $data->inventory->item->unit '),
		array('name' => 'Date', 'value' => '$data->date  ' ),
		array('name' => 'Comment', 'value' => '$data->comment  ' ),
		array('name' => 'User', 'value' => '$data->user->fname." ".$data->user->lname'),
    )
));




	
	
?>


<br>
<br>
<br>
<?php 
$config = array();
$dataProvider = new CArrayDataProvider($rawData=$model->purchaseOrders, $config); ?>
<h1>Purchase Order</h1>
<?php
$this->widget('zii.widgets.grid.CGridView', array('dataProvider'=>$dataProvider, 'columns'=>array(
		array('name'=>'Item', 'header'=> 'Purchase Order No(s)', 'value'=>'$data->id'),

		array(
            'class'=>'CButtonColumn'
            , 'viewButtonUrl'=>'Yii::app()->createUrl("/PurchaseOrder/view", array("id"=>$data["id"]))'
			 , 'updateButtonUrl'=>'Yii::app()->createUrl("/PurchaseOrder/update", array("id"=>$data["id"]))'
            , 'deleteButtonUrl'=>'Yii::app()->createUrl("/PurchaseOrder/delete", array("id"=>$data["id"]))'
           
            )
    )
));




	
	
?>


